package com.camp.android.trip.n.track.menu;

import java.util.ArrayList;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.camp.android.trip.n.track.DatabaseManager;
import com.camp.android.trip.n.track.ItemActivity;
import com.camp.android.trip.n.track.NewItemActivity;
import com.camp.android.trip.n.track.NewTripActivity;
import com.camp.android.trip.n.track.R;
import com.camp.android.trip.n.track.Trip;
import com.camp.android.trip.n.track.trip.TripItineraryActivity;

public class MenuActivity extends FragmentActivity implements
                ActionBar.TabListener {

        /**
         * The {@link android.support.v4.view.PagerAdapter} that will provide
         * fragments for each of the sections. We use a
         * {@link android.support.v4.app.FragmentPagerAdapter} derivative, which
         * will keep every loaded fragment in memory. If this becomes too memory
         * intensive, it may be best to switch to a
         * {@link android.support.v4.app.FragmentStatePagerAdapter}.
         */
        SectionsPagerAdapter mSectionsPagerAdapter;

        /**
         * The {@link ViewPager} that will host the section contents.
         */
        ViewPager mViewPager;

        /**
         * The names of the tabs
         */
        ArrayList<String> tabNameList = new ArrayList<String>();
        
        
        @Override
        public void onCreate(Bundle savedInstanceState) {
                
                super.onCreate(savedInstanceState);
                
                setContentView(R.layout.activity_menu);
                
                // Add tab names
                tabNameList.add(getString(R.string.menu_tab_past).toUpperCase());
                tabNameList.add(getString(R.string.menu_tab_current).toUpperCase());
                tabNameList.add(getString(R.string.menu_tab_upcoming).toUpperCase());
                                
                // Create the adapter that will return a fragment for each of the three
                // primary sections of the app.
                mSectionsPagerAdapter = new SectionsPagerAdapter( getSupportFragmentManager());

                // Set up the action bar.
                final ActionBar actionBar = getActionBar();
                actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

                // Set up the ViewPager with the sections adapter.
                mViewPager = (ViewPager) findViewById(R.id.pager);
                mViewPager.setAdapter(mSectionsPagerAdapter);

                // When swiping between different sections, select the corresponding
                // tab.
                // We can also use ActionBar.Tab#select() to do this if we have a
                // reference to the Tab.
                mViewPager.setOnPageChangeListener(
                                new ViewPager.SimpleOnPageChangeListener() {
                                        @Override
                                        public void onPageSelected(int position) {
                                                actionBar.setSelectedNavigationItem(position);
                                        }
                                });

                // For each of the sections in the app, add a tab to the action bar.
                for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
                        // Create a tab with text corresponding to the page title defined by
                        // the adapter.
                        // Also specify this Activity object, which implements the
                        // TabListener interface, as the
                        // listener for when this tab is selected.
                        actionBar.addTab(actionBar.newTab()
                                        .setText(mSectionsPagerAdapter.getPageTitle(i))
                                        .setTabListener(this));
                }
                
                
                actionBar.selectTab(getActionBar().getTabAt(1)); 
                mViewPager.setCurrentItem(1);
                
                Log.i("TAG","CurrentItem set to " + mViewPager.getCurrentItem());

        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
                getMenuInflater().inflate(R.menu.activity_menu, menu);
                return true;
        }

        public boolean onOptionsItemSelected(MenuItem item) {
                
                // If "new trip" button pressed
                if (item.getItemId() == R.id.new_trip) {
                        // Go to newTripActivity
                        Intent intent = new Intent(this, NewTripActivity.class);
                        startActivity(intent);
                }
                return true;
        }

        @Override
        protected void onResumeFragments() {
                // TODO Auto-generated method stub
                super.onResumeFragments();
                
                // Refresh list
                int currentItem = mViewPager.getCurrentItem();
                mViewPager.setAdapter(mSectionsPagerAdapter);
                mViewPager.setCurrentItem(currentItem);
                
                Log.i("TAG","OnResumeFragments, currentItem: "+ mViewPager.getCurrentItem());
        }
        
        @Override
        public void onTabUnselected(ActionBar.Tab tab,
                        FragmentTransaction fragmentTransaction) {
        }

        @Override
        public void onTabSelected(ActionBar.Tab tab,
                        FragmentTransaction fragmentTransaction) {
                // When the given tab is selected, switch to the corresponding page in
                // the ViewPager and refresh list.
                mViewPager.setAdapter(mSectionsPagerAdapter);
                mViewPager.setCurrentItem(tab.getPosition());
                
                Log.i("TAG", "Tab position: " + tab.getPosition());
        }

        @Override
        public void onTabReselected(ActionBar.Tab tab,
                        FragmentTransaction fragmentTransaction) {
                
        }

        /**
         * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
         * one of the primary sections of the app.
         */
        public class SectionsPagerAdapter extends FragmentPagerAdapter {

                public SectionsPagerAdapter(FragmentManager fm) {
                        super(fm);
                }

                @Override
                public Fragment getItem(int i) {
                        Log.i("TAG","Getting item "+ i);
                        // Init variables
                        Fragment fragment = null;
                        Bundle args = new Bundle();

                        // Switch on tabs and create/load corresponding fragment
                        switch (i) {
                        case 2:
                                Log.i("TAG","case upcoming");
                                fragment = new MenuUpcomingFragment();
                                break;
                        case 1:
                                Log.i("TAG","case current");
                                fragment = new MenuCurrentFragment();
                                break;
                        case 0:
                                Log.i("TAG","case past"); 
                                fragment = new MenuPastFragment();
                                break;

                        default:
                                break;
                        }
                        return fragment;
                }

                @Override
                public int getCount() {
                        return tabNameList.size();
                }

                @Override
                public CharSequence getPageTitle(int position) {
                        return tabNameList.get(position);
                }
        }

        /**
         * A dummy fragment representing a section of the app, but that simply
         * displays dummy text.
         */
        public static class MenuCurrentFragment extends Fragment implements OnClickListener {
                public MenuCurrentFragment() {
                }

                @Override
                public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                Bundle savedInstanceState) {
                	
                	DatabaseManager dbm = new DatabaseManager(getActivity());
					Trip currentTrip = (Trip) dbm.getCurrentTrip();
					
					if(currentTrip != null) {
						Button button = new Button(getActivity());
                		button.setGravity(Gravity.CENTER);
                		button.setHeight(10);
                        button.setText("Go to current trip");
                        button.setOnClickListener(this);
                        return button;
					}
					else {
						TextView textView = new TextView(getActivity());
                        textView.setGravity(Gravity.CENTER);
                        textView.setText("You have no current trip!");
                        return textView;
					}
                		
                }

				@Override
				public void onClick(View v) {
					DatabaseManager dbm = new DatabaseManager(getActivity());
					Trip currentTrip = (Trip) dbm.getCurrentTrip();
					int trip_id = currentTrip.id;
					Intent intent = new Intent(getActivity(),TripItineraryActivity.class);
					intent.putExtra("trip_id", ""+trip_id);
					startActivity(intent);
				}
        }

        public void openTrip(View view) {
                Intent intent = new Intent(this, TripItineraryActivity.class);
                startActivity(intent);
        }

        public void openItem(View view) {
                Intent intent = new Intent(this, ItemActivity.class);
                startActivity(intent);
        }

        public void openNewItem(View view) {
                Intent intent = new Intent(this, NewItemActivity.class);
                startActivity(intent);
        }
        
}